package com.jiantong.web.open.app.news;

import com.jiantong.news.model.ThinktankVideo;
import com.jiantong.news.service.ThinktankVideoReadService;
import com.jiantong.news.service.ThinktankVideoWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 养殖智库视频
 * Created by mz03 on 2017/4/13.
 * @author :zyp
 */
@Slf4j
@OpenBean
public class ThinktankVideoApis {
    @RpcConsumer
    private ThinktankVideoWriteService thinktankVideoWriteService;

    @RpcConsumer
    private ThinktankVideoReadService thinktankVideoReadService;

    @OpenMethod(
            key = "find.thinktankVideo.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public ThinktankVideo findById(Long id){
        Response<ThinktankVideo> response = thinktankVideoReadService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find ThinktankVideo read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    @OpenMethod(
            key = "create.thinktankVideo",
            paramNames = {"thinktankVideo"},
            httpMethods = RequestMethod.POST
    )
    public Long create(ThinktankVideo thinktankVideo){
        Response<Long> response = thinktankVideoWriteService.create(thinktankVideo);
        if(!response.isSuccess()){
            log.error("failed to create thinktankVideo read by thinktankVideo = {}, cause : {}", thinktankVideo, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "query.thinktankVideos.by.type",
            paramNames = {"type"},
            httpMethods = RequestMethod.GET
    )
    public List<ThinktankVideo> findByType(Integer type){
        Response< List<ThinktankVideo>> response = thinktankVideoReadService.finByType(type);
        if (!response.isSuccess()){
            log.error("failed to find thinktankVideos read by type = {}, cause : {}", type, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();

    }

    @OpenMethod(
            key = "find.paging.thinktankVideo",
            paramNames = {"pageNo","pageSize","type"},
            httpMethods = RequestMethod.GET
    )
    public Paging<ThinktankVideo> pagingThinktankVideo(Integer pageNo, Integer pageSize, Integer type){
        Response<Paging<ThinktankVideo>> pagingResponse = thinktankVideoReadService.pagingThinktankVideo(pageNo,pageSize,type);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging thinktankVideo type {}, cause : {}", type, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    @OpenMethod(
            key = "find.paging.think.tank.video.by.keyword",
            paramNames = {"pageNo","pageSize","keyword"},
            httpMethods = RequestMethod.GET
    )
    public Paging<ThinktankVideo> pagingByKeyword(Integer pageNo, Integer pageSize, String keyword){
        Response<Paging<ThinktankVideo>> pagingResponse = thinktankVideoReadService.pagingByKeyword(pageNo,pageSize,keyword);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging thinktankVideo keyword {}, cause : {}", keyword, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }
}
